%%%-------------------------------------------------------------------
%%% @author Rain
%%% @copyright (C) 2022, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 14. 6月 2022 17:34
%%%-------------------------------------------------------------------
-module(obj_route).
-author("Rain").
-include("obj.hrl").
-include("msg_id.hrl").

%% API
-export([route/3]).

route(?MSG_ID_ENTER_MAP, Content, Player = #player{uid = Uid}) ->
    X = proplists:get_value("x", Content),
    Y = proplists:get_value("y", Content),
    case is_integer(X) andalso is_integer(Y) of
        true ->
%%            io:format("Uid(~p): 我请求进入了地图的 {~p, ~p} ~n", [Uid#uid.value, X, Y]),
            ok = gen_server:call(tower_aoi_svr, {enter_map, Uid, X, Y}),
            Msg = [{"x", X}, {"y", Y}],
            {reply, Msg, Player};
        _ ->
            io:format("参数格式不对 ~n"),
            {noreply, Player}
    end;


route(?MSG_ID_LEAVE_MAP, _,  Player = #player{uid = Uid}) ->
%%    io:format("Uid(~p): 我请求离开了地图 ~n", [Uid#uid.value]),
    map_util:leave_map(Uid),
    {noreply, Player};

route(?MSG_ID_SYNC_MAP, Content, Player = #player{uid = Uid}) ->
    X = proplists:get_value("x", Content),
    Y = proplists:get_value("y", Content),
    case is_integer(X) andalso is_integer(Y) of
        true ->
%%            io:format("Uid(~p): 我请求移动到地图的 {~p, ~p} ~n", [Uid#uid.value, X, Y]),
            gen_server:cast(tower_aoi_svr, {sync_map, Uid, X, Y});
        _ ->
            io:format("参数格式不对 ~n")
    end,
    {noreply, Player};

route(MsgId, BinMsg, Player) ->
    io:format("未匹配的消息 MsgId:~p Content:~p ~n", [MsgId, BinMsg]),
    {noreply, Player}.